<?php

namespace app\admin\model;

use think\Model;
use think\Session;

class Preproduct extends Model
{
    // 表名
    protected $name = 'preproduct';
    protected $insert = ['uname'];

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    
    // 追加属性
    protected $append = [
        'classif_text',
        'ctime_text'
    ];
    protected $rule = [
        'storenum'  =>  'require|egt:10',
    ];


    protected function setUnameAttr()
    {
        $a = Session::get('admin');
        return $a['username'];

    }


    public function getClassifList()
    {
        return ['1' => __('Classif 1'),'2' => __('Classif 2'),'3' => __('Classif 3'),'4' => __('Classif 4')];
    }     


    public function getClassifTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['classif'];
        $list = $this->getClassifList();

        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getCtimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['ctime'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCtimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    public function cate()
    {
        return $this->belongsTo('app\admin\model\halt\Cate', 'classif')->setEagerlyType(0);
    }

}
